package com.janlei.concurrent;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;


public class ForkJoinStu {

    public static void fork1() throws InterruptedException, ExecutionException {

        ForkJoinPool pool1 = ForkJoinPool.commonPool();
        System.out.println(11111);
        ForkJoinTask<Integer> submit = pool1.submit(() -> {
            int result = 0;
            try {
                result = fork2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return result;
        });

        Thread.sleep(500);
        System.out.println(submit.get());
        pool1.shutdown();

        System.out.println(222);
        while (true) {
        }
    }

    public static Integer fork2() throws InterruptedException {

        return print();
    }

    public static Integer print() throws InterruptedException {
        Integer result = 0;
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            Thread.sleep(200);
            result += i;
        }

        return result;

    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        fork1();
    }
}
